using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.SideChannels;

public class SideChannelController : MonoBehaviour
{
    public EnvironmentController envCtrler;
    StringLogSideChannel stringChannel;
    EnvironmentSettingChannel environmentSettingChannel;
    EnvironmentStateChannel environmentStateChannel;
    public void Awake()
    {
        envCtrler = GameObject.Find("EnvironmentController").GetComponent<EnvironmentController>();

        stringChannel = new StringLogSideChannel();
        environmentSettingChannel = new EnvironmentSettingChannel(envCtrler);
        environmentStateChannel = new EnvironmentStateChannel(envCtrler);
        // When a Debug.Log message is created, we send it to the stringChannel
        Application.logMessageReceived += stringChannel.SendDebugStatementToPython;

        // The channel must be registered with the SideChannelManager class
        SideChannelManager.RegisterSideChannel(stringChannel);
        SideChannelManager.RegisterSideChannel(environmentSettingChannel);
        SideChannelManager.RegisterSideChannel(environmentStateChannel);
    }

    public void OnDestroy()
    {
        // De-register the Debug.Log callback
        Application.logMessageReceived -= stringChannel.SendDebugStatementToPython;
        if (Academy.IsInitialized)
        {
            SideChannelManager.UnregisterSideChannel(stringChannel);
            SideChannelManager.UnregisterSideChannel(environmentSettingChannel);
            SideChannelManager.UnregisterSideChannel(environmentStateChannel);
        }
    }

    void Update()
    {
        //environmentStateChannel.SendStateMessage();
    }

    public void SendStateMessage()
    {
        if((bool)envCtrler.epm.GetPara(EP.unifiedEpisode))
        {
            environmentStateChannel.SendStateMessage();
        }
            
    }

}